﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Net;
using Microsoft.Win32; 

namespace Utilities
{
    public struct Path
    {
        public static string dbPath;
        public static string logPath;
        public static string documentPath;
    }

    public class CommonOperations
    {
        public static void initPaths(string logPath, string dbPath, string codeBase)
        {
            Utilities.Path.logPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + logPath;
            Utilities.Path.dbPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + dbPath;

            string appSpecificFolderPath = System.IO.Path.GetDirectoryName(dbPath);
            if (!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + appSpecificFolderPath))
                Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + appSpecificFolderPath);

            if(!File.Exists(Utilities.Path.dbPath))
            {
                string path = System.IO.Path.GetDirectoryName(codeBase).Replace("file:\\", "");

                File.Copy(path + "\\Resources\\Db\\Info.mdb", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + dbPath);
            }

            string cloudDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Office Integrator";
            
            if (!Directory.Exists(cloudDirectory))
                Directory.CreateDirectory(cloudDirectory);

            Utilities.Path.documentPath = cloudDirectory;
        }

        public static Version GetInstalledVersion()
        {
            //Load the AppStart config File
            AssemblyName AN = AssemblyName.GetAssemblyName(Assembly.GetEntryAssembly().Location);
            return AN.Version;
        }

        static public void DoSaveContent(Stream source, string fileName)
        {
            FileStream fs = new FileStream(fileName, FileMode.Create);

            DoSaveContent(fs, source);
        }

        static public void DoSaveContent(Stream target, Stream source)
        {
            try
            {
                if (target != null)
                {
                    int nBytes = 2048;
                    int count = 0;
                    Byte[] arr = new Byte[nBytes];

                    do
                    {
                        count = source.Read(arr, 0, nBytes);
                        target.Write(arr, 0, count);

                    } while (count > 0);
                    target.Flush();
                }
            }
            finally
            {
                if (target != null)
                    target.Close();
                if (source != null)
                    source.Close();
            }
        }
    }
}
